package com.chaintor.demo.math;

import org.apache.commons.math3.analysis.solvers.NewtonRaphsonSolver;

/**
 * deltaSimple插值转换成delta的输出形式
 */
public class DeltaSimpleOutput {
    private DeltaSimpleFunction dsf;
    private double te_to_t;
    private double dsimples[];

    /**
     * 使用atm报价构造
     * @param atm
     * @param rr25
     * @param bf25
     * @param rr10
     * @param bf10
     * @param te_to_t
     */
    public DeltaSimpleOutput(double atm,double rr25,double bf25,double rr10,double bf10,double te_to_t){
        this.te_to_t = te_to_t;
        dsf = new DeltaSimpleFunction(atm,rr25,bf25,rr10,bf10,te_to_t);
    }

    /**
     * 使用三次曲线参数构造
     * @param knote
     * @param c
     * @param te_to_t
     */
    public DeltaSimpleOutput(double[] knote,double[][] c,double te_to_t){
        dsf = new DeltaSimpleFunction(knote,c);
        this.te_to_t = te_to_t;
    }

    /**
     * 计算输出delta 0.05 ~ 0.95 的波动率
     * @return
     */
    public double[] outPutValue(){
        double deltas[] = {0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5,0.55,0.6,0.65,0.7,0.75,0.8,0.85,0.9,0.95};
        double dsimples[] = new double[deltas.length];
        double sigma[] = new double[deltas.length];
        NewtonRaphsonSolver solver = new NewtonRaphsonSolver();
        for (int i=0;i<deltas.length;i++){
            DeltaToDeltaSimpleFunction dtodsFunc = new DeltaToDeltaSimpleFunction(dsf,deltas[i],te_to_t);
            dsimples[i] = solver.solve(1000,dtodsFunc,deltas[i]);
            sigma[i] = dsf.value(dsimples[i]);
        }
        this.dsimples = dsimples;
        return sigma;
    }

    public double[] getDsimples(){
        return this.dsimples;
    }
}
